package com.example.newsapp.ui.components.newsComponent.newsList

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.database.databaseModel.NewsDatabaseModel
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave
import com.google.gson.Gson
import kotlinx.coroutines.runBlocking

/**
 * 笑话页新闻列表
 */
@SuppressLint("MutableCollectionMutableState")
@Composable
fun JokeNewsList(modifier: Modifier = Modifier) {
    var navController = LocalNavController.current
    var newsNums = 0
    var newsItem = NewsItems(0, "", "", "", "", 0, "")
    val context = LocalContext.current
    val gson = Gson()

    // 初始化数组
    var newsNumsList by rememberSaveable { mutableStateOf(mutableListOf<Int>()) }

    var jokeNewsList = mutableListOf<NewsItems>()

    val newsDatabaseModel = NewsDatabaseModel(context)

    runBlocking {
        // 获取汽车新闻数
        newsNums = newsDatabaseModel.dateSelect.selectJokeNewsNums()
        // 获取所有汽车新闻
        jokeNewsList = newsDatabaseModel.dateSelect.selectJokeNews()
    }
    // 第一次初始化将获取到的新闻数放在一个数组里
    if (StateSave.jokeNewsInit == 0) {
        newsNumsList = mutableListOf<Int>()
        for (i in 1..newsNums) {
            newsNumsList.add(i)
        }
        StateSave.jokeNewsInit++
    }

    if (StateSave.currentTab2 != StateSave.previousTab2 || StateSave.currentTab == StateSave.previousTab || StateSave.jokeNewsInit == 1) {
        if (!StateSave.isRefresh) {
            if (!StateSave.isNewsDetail && !StateSave.isSearchPage && StateSave.isNewsPage) {
                // 打乱顺序, 从别的页面进来或者刷新完成前不用打乱
                newsNumsList.shuffle()
            }
            StateSave.currentTab2 = StateSave.previousTab2
        }
        StateSave.jokeNewsInit++
    }
    StateSave.isSearchPage = false
    StateSave.isNewsDetail = false
    StateSave.isNewsPage = true
    LazyColumn {
        items(newsNums) {
            // 从数据库中获取新闻
            runBlocking {
                newsItem = jokeNewsList[newsNumsList[it] - 1]
            }
            val newsItemJson = gson.toJson(newsItem)
            // 将获取到的picture值转换为对应的id
            val pictureId = context.resources.getIdentifier(
                newsItem.picture.substringAfter("R.drawable."),
                "drawable",
                context.packageName
            )
            ListItem(
                headlineContent = {
                    Text(
                        text = newsItem.title,
                        style = MaterialTheme.typography.titleMedium
                    )
                },
                supportingContent = {

                },
                trailingContent = {

                },
                modifier = Modifier.clickable {
                    navController.navigate(
                        route = "${PageNavigation.newsDetailsScreen}?newsItemJson=${newsItemJson}"
                    )
                },
                colors = ListItemDefaults.colors(containerColor = MaterialTheme.colorScheme.surfaceContainerLow)
            )
            HorizontalDivider(
                modifier = Modifier.padding(horizontal = 15.dp)
            )
        }
    }
}